<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - coverage.lcov - src/mqtt_client_subscriptions_manager.dart</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - mqtt_client_subscriptions_manager.dart<span style="font-size: 80%;"> (source / <a href="mqtt_client_subscriptions_manager.dart.func.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">coverage.lcov</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">59</td>
            <td class="headerCovTableEntry">62</td>
            <td class="headerCovTableEntryHi">95.2 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2017-10-09</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntryHi">-</td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /*</a>
<span class="lineNum">       2 </span>            :  * Package : mqtt_client
<span class="lineNum">       3 </span>            :  * Author : S. Hamblett &lt;steve.hamblett@linux.com&gt;
<span class="lineNum">       4 </span>            :  * Date   : 30/06/2017
<span class="lineNum">       5 </span>            :  * Copyright :  S.Hamblett
<span class="lineNum">       6 </span>            :  */
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : part of mqtt_client;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : /// A class that can manage the topic subscription process.
<span class="lineNum">      11 </span>            : class SubscriptionsManager extends events.EventDetector {
<span class="lineNum">      12 </span>            :   /// Dispenser used for keeping track of subscription ids
<span class="lineNum">      13 </span>            :   MessageIdentifierDispenser messageIdentifierDispenser =
<span class="lineNum">      14 </span>            :   new MessageIdentifierDispenser();
<span class="lineNum">      15 </span>            : 
<span class="lineNum">      16 </span>            :   /// List of confirmed subscriptions, keyed on the topic name.
<span class="lineNum">      17 </span>            :   Map&lt;String, Subscription&gt; subscriptions = new Map&lt;String, Subscription&gt;();
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span>            :   /// A list of subscriptions that are pending acknowledgement, keyed on the message identifier.
<span class="lineNum">      20 </span>            :   Map&lt;int, Subscription&gt; pendingSubscriptions = new Map&lt;int, Subscription&gt;();
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            :   /// The connection handler that we use to subscribe to subscription acknowledgements.
<span class="lineNum">      23 </span>            :   IMqttConnectionHandler connectionHandler;
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            :   /// Publishing manager used for passing on published messages to subscribers.
<span class="lineNum">      26 </span>            :   PublishingManager publishingManager;
<span class="lineNum">      27 </span>            : 
<span class="lineNum">      28 </span>            :   /// Observable publish messages received map, indexed by topic
<span class="lineNum">      29 </span>            :   Map&lt;String, ChangeNotifier&lt;MqttReceivedMessage&gt;&gt; messagesReceived =
<span class="lineNum">      30 </span>            :   new Map&lt;String, ChangeNotifier&lt;MqttReceivedMessage&gt;&gt;();
<span class="lineNum">      31 </span>            : 
<span class="lineNum">      32 </span>            :   ///  Creates a new instance of a SubscriptionsManager that uses the specified connection to manage subscriptions.
<span class="lineNum">      33 </span>            :   SubscriptionsManager(IMqttConnectionHandler connectionHandler,
<span class="lineNum">      34 </span><span class="lineCov">          2 :       IPublishingManager publishingManager) {</span>
<span class="lineNum">      35 </span><span class="lineCov">          2 :     this.connectionHandler = connectionHandler;</span>
<span class="lineNum">      36 </span><span class="lineCov">          2 :     this.publishingManager = publishingManager;</span>
<span class="lineNum">      37 </span>            :     this
<span class="lineNum">      38 </span><span class="lineCov">          2 :         .connectionHandler</span>
<span class="lineNum">      39 </span><span class="lineCov">          4 :         .registerForMessage(MqttMessageType.subscribeAck, confirmSubscription);</span>
<span class="lineNum">      40 </span>            :     this
<span class="lineNum">      41 </span><span class="lineCov">          2 :         .connectionHandler</span>
<span class="lineNum">      42 </span><span class="lineCov">          4 :         .registerForMessage(MqttMessageType.unsubscribeAck, confirmUnsubscribe);</span>
<span class="lineNum">      43 </span>            :     // Start listening for published messages
<span class="lineNum">      44 </span><span class="lineCov">          2 :     this.listen(</span>
<span class="lineNum">      45 </span><span class="lineCov">          4 :         this.publishingManager, MessageReceived, publishMessageReceived);</span>
<span class="lineNum">      46 </span>            :   }
<span class="lineNum">      47 </span>            : 
<span class="lineNum">      48 </span>            :   /// Registers a new subscription with the subscription manager.
<span class="lineNum">      49 </span>            :   ChangeNotifier&lt;MqttReceivedMessage&gt; registerSubscription(String topic,
<span class="lineNum">      50 </span>            :       MqttQos qos) {
<span class="lineNum">      51 </span><span class="lineCov">          1 :     ChangeNotifier&lt;MqttReceivedMessage&gt; cn = tryGetExistingSubscription(topic);</span>
<span class="lineNum">      52 </span>            :     if (cn == null) {
<span class="lineNum">      53 </span><span class="lineCov">          1 :       cn = createNewSubscription(topic, qos);</span>
<span class="lineNum">      54 </span>            :     }
<span class="lineNum">      55 </span>            :     return cn;
<span class="lineNum">      56 </span>            :   }
<span class="lineNum">      57 </span>            : 
<span class="lineNum">      58 </span>            :   /// Gets a view on the existing observable, if the subscription already exists.
<span class="lineNum">      59 </span>            :   ChangeNotifier&lt;MqttReceivedMessage&gt; tryGetExistingSubscription(String topic) {
<span class="lineNum">      60 </span><span class="lineCov">          2 :     Subscription retSub = subscriptions[topic];</span>
<span class="lineNum">      61 </span>            :     if (retSub == null) {
<span class="lineNum">      62 </span>            :       // Search the pending subscriptions
<span class="lineNum">      63 </span><span class="lineCov">          2 :       for (Subscription sub in pendingSubscriptions.values) {</span>
<span class="lineNum">      64 </span><span class="lineNoCov">          0 :         if (sub.topic.rawTopic == topic) {</span>
<span class="lineNum">      65 </span>            :           retSub = sub;
<span class="lineNum">      66 </span>            :         }
<span class="lineNum">      67 </span>            :       }
<span class="lineNum">      68 </span>            :     }
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :     return retSub != null ? retSub.observable : null;</span>
<span class="lineNum">      70 </span>            :   }
<span class="lineNum">      71 </span>            : 
<span class="lineNum">      72 </span>            :   /// Creates a new subscription for the specified topic.
<span class="lineNum">      73 </span>            :   ChangeNotifier&lt;MqttReceivedMessage&gt; createNewSubscription(String topic,
<span class="lineNum">      74 </span>            :       MqttQos qos) {
<span class="lineNum">      75 </span>            :     try {
<span class="lineNum">      76 </span><span class="lineCov">          1 :       final SubscriptionTopic subscriptionTopic = new SubscriptionTopic(topic);</span>
<span class="lineNum">      77 </span>            :       // Get an ID that represents the subscription. We will use this same ID for unsubscribe as well.
<span class="lineNum">      78 </span>            :       final int msgId =
<span class="lineNum">      79 </span><span class="lineCov">          2 :       messageIdentifierDispenser.getNextMessageIdentifier(&quot;subscriptions&quot;);</span>
<span class="lineNum">      80 </span>            :       // Create a new observable that is used to yield messages
<span class="lineNum">      81 </span>            :       // that arrive for the topic.
<span class="lineNum">      82 </span>            :       final ChangeNotifier&lt;MqttReceivedMessage&gt; observable =
<span class="lineNum">      83 </span><span class="lineCov">          1 :       createObservableForSubscription(subscriptionTopic, msgId);</span>
<span class="lineNum">      84 </span><span class="lineCov">          1 :       final Subscription sub = new Subscription();</span>
<span class="lineNum">      85 </span><span class="lineCov">          1 :       sub.topic = subscriptionTopic;</span>
<span class="lineNum">      86 </span><span class="lineCov">          1 :       sub.qos = qos;</span>
<span class="lineNum">      87 </span><span class="lineCov">          1 :       sub.messageIdentifier = msgId;</span>
<span class="lineNum">      88 </span><span class="lineCov">          2 :       sub.createdTime = new DateTime.now();</span>
<span class="lineNum">      89 </span><span class="lineCov">          1 :       sub.observable = observable;</span>
<span class="lineNum">      90 </span><span class="lineCov">          3 :       pendingSubscriptions[sub.messageIdentifier] = sub;</span>
<span class="lineNum">      91 </span>            :       // Build a subscribe message for the caller and send it off to the broker.
<span class="lineNum">      92 </span><span class="lineCov">          1 :       final MqttSubscribeMessage msg = new MqttSubscribeMessage()</span>
<span class="lineNum">      93 </span><span class="lineCov">          2 :           .withMessageIdentifier(sub.messageIdentifier)</span>
<span class="lineNum">      94 </span><span class="lineCov">          3 :           .toTopic(sub.topic.rawTopic)</span>
<span class="lineNum">      95 </span><span class="lineCov">          2 :           .atQos(sub.qos);</span>
<span class="lineNum">      96 </span><span class="lineCov">          2 :       connectionHandler.sendMessage(msg);</span>
<span class="lineNum">      97 </span><span class="lineCov">          1 :       return sub.observable;</span>
<span class="lineNum">      98 </span>            :     } catch (Exception) {
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :       throw new InvalidTopicException(</span>
<span class="lineNum">     100 </span>            :           &quot;from SubscriptionManager::createNewSubscription&quot;, topic);
<span class="lineNum">     101 </span>            :     }
<span class="lineNum">     102 </span>            :   }
<span class="lineNum">     103 </span>            : 
<span class="lineNum">     104 </span>            :   /// Publish message received
<span class="lineNum">     105 </span>            :   void publishMessageReceived(events.Event&lt;MessageReceived&gt; event) {
<span class="lineNum">     106 </span><span class="lineCov">          3 :     final String topic = event.data.topic.rawTopic;</span>
<span class="lineNum">     107 </span><span class="lineCov">          2 :     if (messagesReceived.containsKey(topic)) {</span>
<span class="lineNum">     108 </span>            :       final MqttReceivedMessage&lt;MqttMessage&gt; msg =
<span class="lineNum">     109 </span><span class="lineCov">          3 :       new MqttReceivedMessage&lt;MqttMessage&gt;(topic, event.data.message);</span>
<span class="lineNum">     110 </span><span class="lineCov">          3 :       messagesReceived[topic].notifyChange(msg);</span>
<span class="lineNum">     111 </span>            :     }
<span class="lineNum">     112 </span>            :   }
<span class="lineNum">     113 </span>            : 
<span class="lineNum">     114 </span>            :   /// Creates an observable for a subscription.
<span class="lineNum">     115 </span>            :   ChangeNotifier&lt;MqttReceivedMessage&gt; createObservableForSubscription(
<span class="lineNum">     116 </span>            :       SubscriptionTopic subscriptionTopic, int msgId) {
<span class="lineNum">     117 </span><span class="lineCov">          1 :     final String topic = subscriptionTopic.rawTopic;</span>
<span class="lineNum">     118 </span>            :     final ChangeNotifier&lt;MqttReceivedMessage&gt; cn =
<span class="lineNum">     119 </span><span class="lineCov">          1 :     new ChangeNotifier&lt;MqttReceivedMessage&gt;();</span>
<span class="lineNum">     120 </span><span class="lineCov">          2 :     messagesReceived[topic] = cn;</span>
<span class="lineNum">     121 </span>            :     return cn;
<span class="lineNum">     122 </span>            :   }
<span class="lineNum">     123 </span>            : 
<span class="lineNum">     124 </span>            :   /// Unsubscribe from a topic
<span class="lineNum">     125 </span>            :   void unsubscribe(String topic) {
<span class="lineNum">     126 </span><span class="lineCov">          1 :     final MqttUnsubscribeMessage unsubscribeMsg = new MqttUnsubscribeMessage()</span>
<span class="lineNum">     127 </span><span class="lineCov">          2 :         .withMessageIdentifier(messageIdentifierDispenser</span>
<span class="lineNum">     128 </span><span class="lineCov">          1 :         .getNextMessageIdentifier(&quot;unsubscriptions&quot;))</span>
<span class="lineNum">     129 </span><span class="lineCov">          1 :         .fromTopic(topic);</span>
<span class="lineNum">     130 </span><span class="lineCov">          2 :     connectionHandler.sendMessage(unsubscribeMsg);</span>
<span class="lineNum">     131 </span>            :   }
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            :   /// Confirms a subscription has been made with the broker. Marks the sub as confirmed in the subs storage.
<span class="lineNum">     134 </span>            :   /// Returns true, always.
<span class="lineNum">     135 </span>            :   bool confirmSubscription(MqttMessage msg) {
<span class="lineNum">     136 </span><span class="lineCov">          1 :     final MqttSubscribeAckMessage subAck = msg as MqttSubscribeAckMessage;</span>
<span class="lineNum">     137 </span><span class="lineCov">          1 :     if (pendingSubscriptions</span>
<span class="lineNum">     138 </span><span class="lineCov">          3 :         .containsKey(subAck.variableHeader.messageIdentifier)) {</span>
<span class="lineNum">     139 </span>            :       final String topic =
<span class="lineNum">     140 </span><span class="lineCov">          4 :           pendingSubscriptions[subAck.variableHeader.messageIdentifier]</span>
<span class="lineNum">     141 </span><span class="lineCov">          1 :               .topic</span>
<span class="lineNum">     142 </span><span class="lineCov">          1 :               .rawTopic;</span>
<span class="lineNum">     143 </span><span class="lineCov">          2 :       subscriptions[topic] =</span>
<span class="lineNum">     144 </span><span class="lineCov">          4 :       pendingSubscriptions[subAck.variableHeader.messageIdentifier];</span>
<span class="lineNum">     145 </span><span class="lineCov">          4 :       pendingSubscriptions.remove(subAck.variableHeader.messageIdentifier);</span>
<span class="lineNum">     146 </span>            :     }
<span class="lineNum">     147 </span>            :     return true;
<span class="lineNum">     148 </span>            :   }
<span class="lineNum">     149 </span>            : 
<span class="lineNum">     150 </span>            :   /// Cleans up after an unsubscribe message is received from the broker.
<span class="lineNum">     151 </span>            :   /// returns true, always
<span class="lineNum">     152 </span>            :   bool confirmUnsubscribe(MqttMessage msg) {
<span class="lineNum">     153 </span><span class="lineCov">          1 :     final MqttUnsubscribeAckMessage unSubAck = msg as MqttUnsubscribeAckMessage;</span>
<span class="lineNum">     154 </span>            :     String subKey;
<span class="lineNum">     155 </span>            :     Subscription sub;
<span class="lineNum">     156 </span><span class="lineCov">          2 :     subscriptions.forEach((String key, Subscription value) {</span>
<span class="lineNum">     157 </span><span class="lineCov">          2 :       if (value.messageIdentifier ==</span>
<span class="lineNum">     158 </span><span class="lineCov">          2 :           unSubAck.variableHeader.messageIdentifier) {</span>
<span class="lineNum">     159 </span>            :         sub = value;
<span class="lineNum">     160 </span>            :         subKey = key;
<span class="lineNum">     161 </span>            :       }
<span class="lineNum">     162 </span>            :     });
<span class="lineNum">     163 </span>            :     // If we have the subscription remove it
<span class="lineNum">     164 </span>            :     if (sub != null) {
<span class="lineNum">     165 </span><span class="lineCov">          2 :       subscriptions.remove(subKey);</span>
<span class="lineNum">     166 </span><span class="lineCov">          2 :       messagesReceived.remove(subKey);</span>
<span class="lineNum">     167 </span>            :     }
<span class="lineNum">     168 </span>            :     return true;
<span class="lineNum">     169 </span>            :   }
<span class="lineNum">     170 </span>            : 
<span class="lineNum">     171 </span>            :   /// Gets the current status of a subscription.
<span class="lineNum">     172 </span>            :   SubscriptionStatus getSubscriptionsStatus(String topic) {
<span class="lineNum">     173 </span>            :     SubscriptionStatus status = SubscriptionStatus.doesNotExist;
<span class="lineNum">     174 </span><span class="lineCov">          2 :     if (subscriptions.containsKey(topic)) {</span>
<span class="lineNum">     175 </span>            :       status = SubscriptionStatus.active;
<span class="lineNum">     176 </span>            :     }
<span class="lineNum">     177 </span><span class="lineCov">          2 :     pendingSubscriptions.forEach((int key, Subscription value) {</span>
<span class="lineNum">     178 </span><span class="lineCov">          3 :       if (value.topic.rawTopic == topic) {</span>
<span class="lineNum">     179 </span>            :         status = SubscriptionStatus.pending;
<span class="lineNum">     180 </span>            :       }
<span class="lineNum">     181 </span>            :     });
<span class="lineNum">     182 </span>            :     return status;
<span class="lineNum">     183 </span>            :   }
<span class="lineNum">     184 </span>            : }
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.10</a></td></tr>
  </table>
  <br>

</body>
</html>
